<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div th:replace="gm/common :: html"></div>
<table id="PermissionTable" title="权限管理" class="easyui-treegrid" style="width: 90%;"
		data-options="
                url: '/menu/getPermission',
                method: 'get',
                rownumbers: true,
                pagination: true,
                pageSize: 30,
                pageList: [30,60,90],
                idField: 'id',
                treeField: 'text',
                fitColumns:true,
                scrollbarSize:0,
                toolbar:toolbar
                
            ">
		<thead>
			<tr>
			    <th data-options="field:'parentId', hidden:true">父菜单ID</th>
				<th data-options="field:'text',align:'left',halign:'center',width:220">权限名称</th>
				<th data-options="field:'href',width:300,align:'left',halign:'center'">请求路径</th>

			</tr>
		</thead>
	</table>
	<div id="addPermissionDialog" class="easyui-dialog" title="添加权限请求"
		data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true"
		style="width: 600px; height: 300px; padding: 20px">
		<div align="center">
		<table style="border-collapse: separate; border-spacing: 0px 10px;">
			<tr>
				<td><span id="errorTips" style="color: red"></span></td>
			</tr>
			<tr>
				<td>父类菜单： <input id = "parentId" class="easyui-combobox" name="language" style="width:80%;" data-options="
                    url:'/menu/getMenuList',
                    method:'get',
                    valueField:'id',
                    textField:'text',
                    panelHeight:'auto',
                    labelPosition: 'top'
                    ">
				</td>
			</tr>
			<tr>
				<td>菜单名称： <input class="easyui-textbox" prompt="权限名称"
					id="name" style="width:80%;">
				</td>
			</tr>

			<tr>
				<td>请求路径： <input class="easyui-textbox" prompt="请求路径"
					id="hrefPath" style="width:80%;">
				</td>
			</tr>
			<tr align="center">
				<td><a href="javascript:void(0)" class="easyui-linkbutton"
					onclick="submitAddPermission()">--确认添加--</a></td>
			</tr>
		</table>
		</div>
	</div>

	<script>
	 
		var toolbar = [ {
			text : '添加权限',
			iconCls : 'icon-add',
			handler : function() {
				$("#addPermissionDialog").dialog("open");
			}
		}, '-', {
			text : '删除权限',
			iconCls : 'icon-remove',
			handler : deletePermission
		} ];
		function deletePermission(){
			var row = $("#PermissionTable").datagrid("getSelected");
			if(row == null){
				$.messager.alert('提示','请选择要删除的权限行','error');
				return;
			}
			$.messager.confirm('警告', '确定要删除' + row.text + '权限吗？', function(r) {
				if (r) {
					var id = row.id;
					var parentId = row.parentId;
					postAjax("/menu/deletePermission/" + id + "/" + parentId,{},function(data){
						if(data.code == 0){
							$("#PermissionTable").treegrid('reload');
							$.messager.alert('提示','删除权限成功');
						} else {
							$.messager.alert('提示','服务器错误，请联系开发者:' + data.msg,'error');
						}
					});
				}
			});
			
		}
		function submitAddPermission(){
			var parentId = $("#parentId").combobox("getValue");
			if(parentId == "-1"){
				$.messager.alert('提示','请选择父类菜单,否则不能添加权限','error');
				return;
			}
			var name = $("#name").val();
			var hrefPath = $("#hrefPath").val();
			var url = "/menu/addPermission";
			var json = {"text":name,"href":hrefPath,"parentId":parentId};
			postAjax(url,json,function(data){
				if(data.code == 0){
					$("#addPermissionDialog").dialog("close");
					$("#PermissionTable").treegrid('reload');//大括号内是需要的参数
					$.messager.alert('提示','添加菜单成功');
				} else {
					$.messager.alert('提示','服务器错误，请联系开发者:' + data.msg,'error');
				}
			});
			
		}
	</script>

</body>
</html>